package cn.datax.service.data.market.controller;

import cn.datax.common.base.BaseController;
import cn.datax.common.core.JsonPage;
import cn.datax.common.core.R;
import cn.datax.common.validate.ValidationGroups;
import cn.datax.service.data.market.api.query.DataShareQuery;
import cn.datax.service.data.market.cata.dto.CataDto;
import cn.datax.service.data.market.cata.dto.CataResourceRelaDto;
import cn.datax.service.data.market.cata.entity.CataEntity;
import cn.datax.service.data.market.cata.entity.CataResourceRelaEntity;
import cn.datax.service.data.market.cata.entity.ResourceInfoEntity;
import cn.datax.service.data.market.cata.vo.CataVo;
import cn.datax.service.data.market.cata.vo.ResourceInfoVo;
import cn.datax.service.data.market.mapstruct.CataMapper;
import cn.datax.service.data.market.mapstruct.ResourceInfoMapper;
import cn.datax.service.data.market.service.CataResourceService;
import cn.datax.service.data.market.service.CataService;
import cn.datax.service.data.market.table.query.DataTableQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuwei
 * @since 2019-09-04
 */
@Api(value="数据目录管理接口", tags = {"数据目录管理"})
@RestController
@RequestMapping("/cata")
@Slf4j
public class CataController extends BaseController {

    @Autowired
    private CataService cataService;

    @Autowired
    private CataResourceService resourceService;

    @Autowired
    private CataMapper cataMapper;

    @Autowired
    private ResourceInfoMapper resourceMapper;

    @ApiOperation(value = "获取数据目录详细信息", notes = "根据url的id来获取数据目录详细信息")
    @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public R getCataById(@PathVariable String id) {
        CataEntity cataEntity = cataService.getById(id);
        return R.ok().setData(cataMapper.toVO(cataEntity));
    }

    @ApiOperation(value = "获取目录列表", notes = "")
    @GetMapping("/list")
    public R getCataList() {
        List<CataEntity> list = cataService.list(Wrappers.emptyWrapper());
        List<CataVo> collect = list.stream().map(cataMapper::toVO).collect(Collectors.toList());
        return R.ok().setData(collect);
    }

    @ApiOperation(value = "创建目录", notes = "根据cata对象创建目录")
    @ApiImplicitParam(name = "cata", value = "目录详细实体cata", required = true, dataType = "CataDto")
    @PostMapping()
    public R saveCata(@RequestBody @Validated({ValidationGroups.Insert.class}) CataDto cata) {
        CataEntity cataEntity = cataService.saveCata(cata);
        return R.ok().setData(cataMapper.toVO(cataEntity));
    }

    @ApiOperation(value = "更新部门详细信息", notes = "根据url的id来指定更新对象，并根据传过来的cata信息来更新部门详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "cata", value = "部门详细实体cata", required = true, dataType = "CataDto")
    })
    @PutMapping("/{id}")
    public R updateCata(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) CataDto cata) {
        CataEntity cataEntity = cataService.updateCata(cata);
        return R.ok().setData(cataMapper.toVO(cataEntity));
    }

    @ApiOperation(value = "删除部门", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public R deleteCata(@PathVariable String id) {
        cataService.deleteCataById(id);
        return R.ok();
    }


    @ApiOperation(value = "分页查询获取目录下资源", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataShareQuery", value = "查询实体dataShareQuery", required = true, dataTypeClass = DataShareQuery.class)
    })
    @GetMapping("/page")
    public R getCataResourceList(DataShareQuery dataShareQuery) {
        QueryWrapper<ResourceInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(dataShareQuery.getCataId()), "cata_id", dataShareQuery.getCataId());
        queryWrapper.eq(StrUtil.isNotBlank(dataShareQuery.getResourceType()), "resource_type", dataShareQuery.getResourceType());
        queryWrapper.like(StrUtil.isNotBlank(dataShareQuery.getResourceName()), "resource_Name", dataShareQuery.getResourceName());
        IPage<ResourceInfoEntity> page = resourceService.pageGetResourceByCataId(new Page<>(dataShareQuery.getPageNum(), dataShareQuery.getPageSize()), dataShareQuery.getCataId(), dataShareQuery.getResourceType(), dataShareQuery.getResourceName());

        if(null != page) {
            List<ResourceInfoVo> collect = page.getRecords().stream().map(resourceMapper::toVO).collect(Collectors.toList());
            JsonPage<ResourceInfoVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
            return R.ok().setData(jsonPage);
        }else{
            return R.ok().setData(new JsonPage<ResourceInfoVo>(1l, 1l, 0l, new ArrayList<ResourceInfoVo>()));
        }
    }

    @ApiOperation(value = "绑定目录资源关系", notes = "")
    @PostMapping("/bindCataResource")
    public R bindCataResource(@RequestBody @Validated({ValidationGroups.Insert.class}) CataResourceRelaDto cata) {
        return R.ok().setData(resourceService.saveRelation(cata));
    }
}

